<template>
	<view class="order-content">
		<!-- <u-navbar back-text="" title="订单中心"></u-navbar> -->
		<view class="search-input">
			<u-search placeholder="输入订单号搜索" v-model="queryForm.orderNo" @custom='searchData' bg-color="#fff"
				:show-action="true">
			</u-search>
		</view>
		<u-tabs :list="list" :is-scroll="false" :current="typeCurrent" @change='changeType'></u-tabs>
		<scroll-view scroll-y="true" style="height: calc(100vh - 200rpx);" @scrolltolower='scrolltolower'>
			<view class="order-list" v-for="item in orderList" :key="item.id">
				<u-card padding="20">
					<view class="cardTop" slot="head">
						<view class="cardTop_left">
							<view class="cardTop_left_orderNum">订单号: <text>{{item.orderNo}}</text></view>
							<view>付款时间: <text>{{item.successTime}}</text></view>
						</view>
						<view class="cardTop_right">
							{{item.orderStatus == 2 ? '待入住' : item.orderStatus == 4 ? '已退款' : '可提现'}}
						</view>
					</view>
					<view class="cardCenter_1" slot="body">
						<view class="img_left">
							<image :src="item.houseInfo.images[0]" mode="aspectFill"></image>
						</view>

						<view class="right_title">
							<view class="house-title">{{item.houseInfo.title}}</view>
							<view class="house-des">
								<view>户型：{{item.houseInfo.tags}}</view>
								<view>区域：{{item.houseInfo.area}}</view>
								<view>中介费：{{item.houseInfo.isAgency== 1 ? "有" : '无'}}</view>
							</view>
							<view class="house-time-price">
								<view>租期：{{item.skuInfo.title}}</view>
								<view>￥{{item.skuInfo.price}}/月</view>
							</view>
						</view>
					</view>
					<view class="cardCenter_2" slot="body" v-if="item.orderStatus != 1">
						<view class="content" @click="callPhone(item.releaseInfo.phone)">
							<text>房源商：{{item.releaseInfo.realName || item.releaseInfo.nickName }}</text>
							<text style="color: #1296db;">联系方式:{{item.releaseInfo.phone || '无'}}</text>
						</view>
						<view class="content" @click="callPhone(item.intermediaryInfo.phone || '15585112020')">
							<text>销售员：{{item.intermediaryInfo.realName || item.intermediaryInfo.nickName || '林羽晨'}}</text>
							<text style="color: #1296db;">联系方式:{{item.intermediaryInfo.phone || '15585112020'}}</text>
						</view>
						<view class="content" @click="callPhone(item.createErInfo.phone)">
							<text>租户：{{item.createErInfo.realName || item.createErInfo.nickName}}</text>
							<text style="color: #1296db;">联系方式:{{item.createErInfo.phone || '无'}}</text>
						</view>
						<view class="content" v-if="item.houseInfo.housePassword != ''">
							<text>开房密码</text>
							<text style="color: #1296db;">{{item.houseInfo.housePassword}}</text>
						</view>
					</view>
					<!-- <view class="order-address" slot="body" @click="openMap(item.houseInfo)">
						<text>地址：{{item.houseInfo.address}} </text>
						<text><u-icon name="arrow-right" color="#1296db" size="28"></u-icon>
						</text>
					</view> -->
					<view class="cardBottom" slot="body">
						<view class="income-box">
							预计收益：<text>￥{{item.withdrawalMoney < 0 ? 0 : item.withdrawalMoney}}</text> </view>
						<view class="btn-list">
							<view @click="withdrawal(item)" v-if="item.orderStatus == 5 && item.isWithdrawal != 1">提现</view>
							<view v-if="item.orderStatus == 5 && item.isWithdrawal == 1">已经提现</view>
						</view>
					</view>
				</u-card>
			</view>
		</scroll-view>

		<view style="width: 100%; height: 20rpx;">

		</view>
	</view>
</template>

<script>
	import {
		GetWithdrawalOrderList,
		CreateOrder,
		PayToBankCard
	} from '@/api/index.js'
	export default {
		data() {
			return {
				typeCurrent: 0,
				orderList: [],
				queryForm: {
					pageSize: 10,
					pageNum: 1,
					orderStatus: 2,
					openId: '',
					orderNo: '',
					type: ''
				},
				list: [{
						name: '待入住' //已付款
					}, {
						name: '可提现' //已完成
					},
					{
						name: '已退款'
					},
				],
				totle: 0,
				current: 0,
			}
		},
		onLoad() {
			this.queryForm.openId = uni.getStorageSync('userInfo').openId
			this.queryForm.type = uni.getStorageSync('userInfo').userType
		},
		onShow() {
			this.orderList = []
			this.getOrderList()
		},
		methods: {
			callPhone(phoneNumber) {
				uni.makePhoneCall({
					phoneNumber //仅为示例
				});
			},
			// 打开地图
			openMap(item) {
				item.position = item.position.split(",")
				item.position.forEach((ele, index) => {
					item.position[index] = parseFloat(item.position[index])
				})
				uni.openLocation({
					latitude: item.position[1],
					longitude: item.position[0],
					success: function() {
						console.log('success');
					},
					fail(error) {
						console.log(error, 'error');
					}
				});
			},
			scrolltolower() {
				if (this.totle == this.orderList.length) {
					return
				}
				this.queryForm.pageNum += 1
				this.getOrderList()
			},
			getOrderList() {
				this.queryForm.openId = uni.getStorageSync('userInfo').openId
				GetWithdrawalOrderList(this.queryForm).then(res => {
					console.log(res.data.records, '订单数据');
					this.orderList = [...this.orderList, ...res.data.records]
					this.orderList.forEach(ele => {
						ele.houseInfo.images = ele.houseInfo.images.split(',')
					})
					this.totle = res.data.totle
				})
			},

			searchData() {
				this.orderList = []
				this.getOrderList()
			},
			changeType(index) {
				this.typeCurrent = index
				if (index == 0) {
					this.queryForm.orderStatus = 2
				}
				if (index == 1) {
					this.queryForm.orderStatus = 5
				}
				if (index == 2) {
					this.queryForm.orderStatus = 4
				}
				// this.queryForm.orderStatus = index
				this.queryForm.pageNum = 1
				this.orderList = []
				this.getOrderList()
			},
			withdrawal(item) {
				let that = this
				let userInfo = uni.getStorageSync('userInfo')
				if (!userInfo.bankCode && !userInfo.bankName && !userInfo.bankNo && !userInfo.bankRealName) {
					uni.showModal({
						title: '提现提示',
						content: '你还未绑定银行信息，无法提现，点击确定去绑定',
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/personCenter/index'
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
					return
				}
				uni.showModal({
					title: '提现提示',
					content: '是否提现该订单?',
					success: function(res) {
						if (res.confirm) {
							// console.log('用户点击确定');
							PayToBankCard({
								openId: uni.getStorageSync('userInfo').openId,
								orderNo: item.orderNo
							}).then(res => {
								uni.showToast({
									title: '提现成功，等待到账',
									duration: 800
								})
								that.queryForm = {
									pageSize: 10,
									pageNum: 1,
									orderStatus: 4,
									openId: '',
									orderNo: '',
									type: ''
								}
								this.typeCurrent = 2
								that.orderList = []
								this.getOrderList()
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.search-input {
		padding: 20rpx;
		// background-color: #fff;
	}

	.cardTop {
		display: flex;
		justify-content: space-between;
		// border-bottom: red solid 1px;
		padding-bottom: 3px;
		padding-top: 3px;

		.cardTop_left {
			.cardTop_left_orderNum {
				margin-bottom: 10rpx;
			}


		}

		.cardTop_right {
			color: #1296db;
		}
	}

	.cardCenter_1 {
		border-bottom: #e4e7ed solid 1px;
		padding-bottom: 3px;
		padding-top: 3px;
		display: flex;
		align-items: center;

		.img_left {
			margin-right: 20rpx;

			image {
				width: 120rpx;
				flex: 0 0 120rpx;
				height: 120rpx;
				border-radius: 8rpx;
				margin-left: 12rpx;
			}
		}

		.right_title {
			font-size: 22rpx;
			// height: 120rpx;
			width: 100%;
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.house-title {
				font-size: 30rpx;
				font-weight: bold;
			}

			.house-des {
				display: flex;
				justify-content: space-between;


				>view {
					padding: 3rpx 5rpx;
					background-color: #1296db;
					border-radius: 10rpx;
					color: #fff;
				}
			}

			.house-time-price {
				display: flex;
				justify-content: space-between;

				>view:nth-child(1) {
					padding: 3rpx 5rpx;
					background-color: #1296db;
					border-radius: 10rpx;
					color: #fff;
				}

				>view:nth-child(2) {
					color: #d60404;
				}
			}
		}

	}

	.cardCenter_2 {
		border-bottom: #e4e7ed solid 1px;
		padding-bottom: 3px;
		padding-top: 3px;

		.content {
			display: flex;
			justify-content: space-between;
		}
	}

	.cardBottom {
		display: flex;
		justify-content: space-between;
		margin-top: 10rpx;


		.income-box {

			flex: 1;
			border-right: 2rpx solid #e4e7ed;

			>text {
				color: #d60404;
			}
		}

		.btn-list {
			flex: 1;
			display: flex;
			justify-content: space-around;

			>view {
				padding: 5rpx 20rpx;
				background-color: #1296db;
				color: #fff;
				border-radius: 15rpx;
			}
		}
	}
</style>